package com.hualan.sfzyproject.controller;

import com.hualan.sfzyproject.domain.*;
import com.hualan.sfzyproject.mapper.CoursesMapper;
import com.hualan.sfzyproject.service.CoursesService;
import com.hualan.sfzyproject.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName CoursessController
 * @Description TODO
 * @Author libowen
 * @Date 2025年01月03日 11:16
 * @Version 1.0
 */
@RestController
@RequestMapping("admin/courses")
public class CoursesController {
    @Autowired
    private CoursesService coursesService;
    @Autowired
    private CoursesMapper coursesMapper;
    @PostMapping
    public R add(@RequestBody Courses courses) {
        return coursesService.insert(courses) != 0 ?R.success("添加成功"):R.error("添加失败");
    }
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        return coursesService.delete(id) != 0 ?R.success("删除成功"):R.error("删除失败");
    }
    @PutMapping("update/{id}")
    public R update(@RequestBody Courses courses) {
        return coursesService.update(courses) != 0 ?R.success("修改成功"):R.error("修改失败");
    }
    @GetMapping("list")
    public R findAll(Integer page,Integer pageSize) {
        return R.ok(coursesService.selectAll(page,pageSize));
    }
    @GetMapping("detail/{id}")
    public R findById(@PathVariable("id") Long id) {
        return R.ok(coursesService.selectBycoursesId(id));
    }
    @GetMapping("search/{name}")
    public R findByName(@PathVariable("name") String name) {
        return R.ok(coursesMapper.selectByName(name));
    }
    @DeleteMapping("/batch")
    public R batchDelete(@RequestBody Map<String, List<Long>> params) {
        List<Long> courseIds = params.get("courseIds");
        if (courseIds == null || courseIds.isEmpty()) {
            return R.error("未选择要删除的课程");
        }
        return coursesService.batchDelete(courseIds) ? R.success("批量删除成功") : R.error("批量删除失败");
    }
        @GetMapping("/search")
        public R search(
                @RequestParam(required = false) String keyword,
                @RequestParam(defaultValue = "1") Integer page,
                @RequestParam(defaultValue = "5") Integer pageSize) {
            try {
                return R.ok(coursesService.search(keyword, page, pageSize));
            } catch (Exception e) {
                return R.error("搜索失败: " + e.getMessage());
            }
        }
        //热门课程
        @GetMapping("/hot")
        public R hotCourses(int page,int pageSize) {
            return R.ok(coursesService.hotCourses(page,pageSize));
        }

        //根据综合排序，人气，好评率，价格，价格区间和分类排序查询
        @GetMapping("/type")
        public R getCoursesByHotAndTypeId(String type,Long typeId, Integer page, Integer pageSize, Integer minPrice, Integer maxPrice) {
            return R.ok(coursesService.selectAllCoursesAndRank(type,typeId,page,pageSize,minPrice,maxPrice));
        }


        //查询是否已收藏
        @GetMapping("/checkCollect")
        public R checkCollect(@RequestParam Integer userId, @RequestParam Integer courseId) {
            return R.success(coursesService.checkCourseCollect(userId, courseId));
        }
        //删除收藏
        @DeleteMapping("/deleteCollect")
        public R deleteCollect(@RequestParam Integer userId, @RequestParam Integer courseId) {
            return coursesMapper.deleteCourseCollect(userId, courseId) ? R.success("取消收藏成功") : R.error("取消收藏失败");
        }
        //添加收藏
        @PostMapping("/collect")
        public R collect(@RequestBody CourseCollection courseCollection) {
            return coursesService.addCourseCollect(courseCollection) ? R.success("收藏成功") : R.error("收藏失败");
        }
        //查询课件列表
        @GetMapping("/detailList")
        public R getCourseInformation(@RequestParam Integer id) {
            return R.ok(coursesMapper.findList(id));
        }
        //名师介绍
        @GetMapping("/detailIntroduce")
        public R getIntr(@RequestParam Integer id) {
            return R.ok(coursesMapper.findIntroduce(id));
        }
//        //课堂交流
//        @GetMapping("/detailQuestion")
//        public R getQue(Integer id,Integer page,Integer pageSize) {
//        return R.ok(coursesService.findCommunication(id,page,pageSize));
//    }
        //课程评价
        @GetMapping("/detailReviews")
        public R getReviews(Integer courseId,Integer userId) {
            return R.ok(coursesMapper.findReviews(courseId,userId));
        }
        //查询课程是否购买
        @GetMapping("/buyCheck")
        public R buyCheck(@RequestParam Integer courseId,@RequestParam Integer userId) {
            return R.success(coursesMapper.checkBuy(courseId,userId));
        }
        //课程购买
        @PostMapping("/buy")
        public R buy(@RequestBody CoursePurchaseRecord coursePurchaseRecord) {
            Integer integer = coursesService.buyCourse(coursePurchaseRecord);
            return R.ok(integer);
        }
        //收藏查询已支付课程
        @GetMapping("/collectionChecked")
        public R collectionChecked(@RequestParam Integer userId) {
            return R.ok(coursesMapper.findPayCourse(userId));
        }
        //收藏查询未支付课程
        @GetMapping("/collectionNoChecked")
        public R collectionNoChecked(@RequestParam Integer userId) {
            return R.ok(coursesMapper.findNoPayCourse(userId));
        }
        //添加课程评价
        @PostMapping("/addReviews")
        public R addReviews(@RequestBody CourseReviews courseReviews) {
            return coursesService.addReview(courseReviews) ? R.success("添加成功") : R.error("添加失败");
        }
        //查询课程交流问题
        @GetMapping("/queryQuestion")
        public R queryQuestion(@RequestParam Integer courseId,@RequestParam Integer page,@RequestParam Integer pageSize) {
            return R.ok(coursesService.findQuestion(courseId,page,pageSize));
        }
        //查看回复
        @GetMapping("/reply")
        public R getReply(@RequestParam Integer questionId,@RequestParam Integer courseId,@RequestParam Integer page,@RequestParam Integer pageSize) {
            return R.ok(coursesService.findReply(questionId,courseId,page,pageSize));
        }
        //添加回复
        @PostMapping("/addReply")
        public R addReply(@RequestBody CourseCommunication courseCommunication) {
            return coursesService.addReply(courseCommunication) ? R.success("添加成功") : R.error("添加失败");
        }





}
